package com.rbac.api.controller.lessOne;

import com.rbac.api.controller.request.*;
import com.rbac.api.service.RolesCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;



@RestController
public class RolesController {



    ///角色service
    @Autowired
    private RolesCommonService rolesCommonService;


    ///获取角色列表
    @ResponseBody
    @RequestMapping(value = "/admin/check/roles/getList",method = RequestMethod.GET)
    public Object getRolesList(@Valid PostRolesListRequest rolesListRequest, BindingResult bindingResult) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return rolesCommonService.getRolesListService(rolesListRequest);
    }

    ///获取可操作的菜单list
    @ResponseBody
    @RequestMapping(value = "/admin/common/roles/actionMenusList",method = RequestMethod.POST)
    public Object actionMenusList() throws Exception{
        ///
        return rolesCommonService.actionMenusListService();
    }


    ///获取可操作的管理员list
    @ResponseBody
    @RequestMapping(value = "/admin/common/roles/actionUsersList",method = RequestMethod.POST)
    public Object actionUsersList() throws Exception{
        ///
        return rolesCommonService.actionUsersListService();
    }



    ///新增角色
    @ResponseBody
    @RequestMapping(value = "/admin/common/roles/add",method = RequestMethod.POST)
    public Object roleAdd(@Valid @RequestBody PostRoleAddRequest roleAddRequest, BindingResult bindingResult, HttpServletRequest request) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return rolesCommonService.roleAddService(roleAddRequest,request);
    }


    ///编辑角色（获取角色信息）
    @ResponseBody
    @RequestMapping(value = "/admin/common/roles/getRoleInfo",method = RequestMethod.POST)
    public Object getRoleInfo(@Valid @RequestBody PostRoleGetInfoRequest roleGetInfoRequest, BindingResult bindingResult) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return rolesCommonService.getRoleInfoService(roleGetInfoRequest);
    }

    ///编辑角色（保存角色信息）
    @ResponseBody
    @RequestMapping(value = "/admin/common/roles/saveRoleInfo",method = RequestMethod.POST)
    public Object saveRoleInfo(@Valid @RequestBody PostRoleSaveRequest roleSaveRequest, BindingResult bindingResult, HttpServletRequest request) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return rolesCommonService.saveRoleInfoService(roleSaveRequest,request);
    }



    ///角色上线下线
    @ResponseBody
    @RequestMapping(value = "/admin/common/roles/saveStatus",method = RequestMethod.POST)
    public Object roleSaveStatus(@Valid @RequestBody PostRoleSaveStatusRequest roleSaveStatusRequest, BindingResult bindingResult, HttpServletRequest request) throws Exception{
        //如果校验存在错误
        if(bindingResult.hasErrors()){
            throw new MethodArgumentNotValidException(null,bindingResult);
        }
        ///
        return rolesCommonService.roleSaveStatusService(roleSaveStatusRequest,request);
    }










}
